package dataStructure.xmg2.a01;

import dataStructure.xmg2.a01.tools.Asserts;
import dataStructure.xmg2.a01.tools.Integers;
import dataStructure.xmg2.a01.tools.Times;
import dataStructure.xmg2.a02.InsertionSort1;
import dataStructure.xmg2.a02.InsertionSort2;

import java.util.Arrays;

/**
 * @author: vince_zww
 * @date: 2021/12/17 21:27
 * @desceription: 冒泡排序，
 */
public class Main {
    public static void main(String[] args) {
//        int[] array = {10, 9,56, 19, 28, 37, 34};

//        Integer[] array1 = Integers.ascOrder(1, 1000000);
//        Integer[] array2 = Integers.copy(array1);
//        Integer[] array3 = Integers.copy(array1);
//
//        Times.test("bubbleSort2",()->{
//            bubbleSort2(array2);
//        });
//
//        Times.test("bubbleSort3",()->{
//            bubbleSort2(array3);
//        });

        Integer[] array1 = Integers.random(10000, 1, 20000);
//        Integer[] array2 = Integers.copy(array1);
//        Integer[] array3 = Integers.copy(array1);
        testSorts(array1,
//                new BubbleSort1(),
//                new BubbleSort2(),
                new HeapSort(),
                new BubbleSort3(),
                new InsertionSort1(),
                new InsertionSort2(),
                new SelectionSort());


//        counttestSort(array1, array2, array3);

//        Asserts.test(Integers.isAscOrder(array));


    }

    static void testSorts(Integer[] array, Sort... sorts) {
        for (Sort sort : sorts) {
            Integer[] newArray = Integers.copy(array);
            sort.sort(newArray);
            Asserts.test(Integers.isAscOrder(newArray));
//            System.out.println(sort);
        }
        Arrays.sort(sorts);

        for (Sort sort : sorts) {
            System.out.println(sort);
        }

    }


    /**
     * 调用各个排序方法，并且对比
     * @param array1
     * @param array2
     * @param array3
     */
    private static void counttestSort(Integer[] array1, Integer[] array2, Integer[] array3) {
        Times.test("HeapSort",() ->{
            new HeapSort().sort(array1);
        });

        Times.test("SelectionSort",() ->{
            new SelectionSort().sort(array2);
        });

        Times.test("BubbleSort3",() ->{
            new BubbleSort3().sort(array3);
        });
    }

    static void testSorts(Sort[] sorts) {

    }






    //记录最后一次交互的位置，后面已经是有序的了！
    private static void bubbleSort3(Integer[] array) {
        for (int end = array.length - 1; end > 0; end--) {
            //sortedIndex的初始值在数组完全有序的时候有用
            int sortedIndex = 1;
            for (int begin = 1; begin <= end; begin++) {
                if (array[begin] < array[begin - 1]) {
                    int tmp = array[begin];
                    array[begin] = array[begin - 1];
                    array[begin - 1] = tmp;

                    sortedIndex = begin;
                }
            }
            end = sortedIndex;
        }
    }


}
